/*
 * Copyright (c) 2009, ConnectCode Pte Ltd
 *                     http://www.webextractor360.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of ConnectCode nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ConnectCode Pte Ltd ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL ConnectCode Pte Ltd BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using WebExtractor360.look4me.Model;
using WebExtractor360.look4me.Action;
using WebExtractor360.look4me.Engine;

namespace WebExtractor360.look4me.Framework
{
    public class Singleton
    {

        private static Singleton m_instance;
        private Singleton()
        {
            m_AppImagePath = "";
            m_AppTemplatePath = "";
        }

        private System.Collections.ArrayList m_frameWorkFormController;

        public void AddForm(string name,System.Windows.Forms.Form value)
        {
            FormHolder fHolder=new FormHolder();
            fHolder.FormName=name;
            fHolder.FormObject=value;
            m_frameWorkFormController.Add(fHolder);
        }

        public void RemoveForm(string name)
        {
            //m_frameWorkFormController.Remove(formobject);
            System.Collections.IEnumerator ie = m_frameWorkFormController.GetEnumerator();
            while (ie.MoveNext())
            {
                FormHolder fHolder = (FormHolder)ie.Current;
                if (fHolder.FormName.Equals(name))
                {
                    m_frameWorkFormController.Remove(fHolder);
                    return;
                }
            }
        }

        public System.Windows.Forms.Form GetForm(string name)
        {
            System.Collections.IEnumerator ie=m_frameWorkFormController.GetEnumerator();

            while (ie.MoveNext())
            {
                FormHolder fHolder=(FormHolder) ie.Current;
                if (fHolder.FormName.Equals(name))
                    return fHolder.FormObject;
            }
            return null;
        }

        static public Singleton GetInstance()
        {
            if (m_instance == null)
            {
                m_instance = new Singleton();

                m_instance.m_AppLogPath = AppPath + @"logs\";
                m_instance.m_AppProfilesPath = AppPath + @"Profiles\";
                m_instance.m_AppEmptyPath = AppPath + @"empty\";

                m_instance.m_frameWorkFormController = new System.Collections.ArrayList();

                DeSerializeAction dseAction = new DeSerializeAction();
                dseAction.Execute();
                ClientModelHolder dseholder = dseAction.GetResult();
                if (dseholder == null)
                    m_instance.m_clientModelHolder = new ClientModelHolder();
                else
                    m_instance.m_clientModelHolder = dseholder;

                //backward compatible reaons.
                if (m_instance.m_clientModelHolder.ShareSearchOptionsVO == null)
                {
                    m_instance.m_clientModelHolder.ShareSearchOptionsVO = new SearchOptionsValueObject();
                    m_instance.m_clientModelHolder.ShareSearchOptionsVO.EmailStrongMatch = true;
                    m_instance.m_clientModelHolder.ShareSearchOptionsVO.IgnoreCriterion = false;
                    m_instance.m_clientModelHolder.ShareSearchOptionsVO.ReportLinksFound = true;
                }

                ReadPageExtensions();

            }
            return m_instance;
        }

        public static void ReadPageExtensions()
        {
            StreamReader freader=null;
            try
            {
                freader = File.OpenText(Singleton.AppPath+ @"\PageExtensionsSupported.txt");
                ArrayList al=new ArrayList();
                string text_line;
                while ((text_line = freader.ReadLine()) != null)
                {
                    
                    al.Add(text_line);
                }
                freader.Close();
                IEnumerator ie = al.GetEnumerator();
                WebProcessingEngine.m_validPageExtensions = new string[al.Count];
                int counter=0;
                while (ie.MoveNext())
                {
                    WebProcessingEngine.m_validPageExtensions[counter++] = (string)ie.Current;
   
                }
                
            }
            catch (Exception)
            {
                throw;
            }
            
        }

        private ClientModelHolder m_clientModelHolder;

        public ClientModelHolder ShareClientModelHolder
        {
            get { return m_clientModelHolder; }
            set { m_clientModelHolder = value; }
        }


        public static string AppPath = "";

        private string m_AppImagePath;
        public string AppImagePath
        {
            get { return m_AppImagePath; }
        }

        private string m_AppTemplatePath;
        public string AppTemplatePath
        {
            get { return m_AppTemplatePath; }
        }

        private string m_AppLogPath;
        public string AppLogPath
        {
            get { return m_AppLogPath; }
        }

        private string m_AppEmptyPath;
        public string AppEmptyPath
        {
            get { return m_AppEmptyPath; }
        }

        private string m_AppDownloadPath;
        public string AppDownloadPath
        {
            get { return m_AppDownloadPath; }
            set { m_AppDownloadPath=value; }

        }

        private string m_AppProfilesPath;
        public string AppProfilesPath
        {
            get { return m_AppProfilesPath; }
        }
 
    }
}
